package com.brillilab.service.logic.instrument.converter;


import com.brillilab.common.utils.CodeUtil;
import com.brillilab.domain.po.instrument.InstrumentMaintenance;
import com.brillilab.domain.vo.instrument.MaintenanceVo;
import com.brillilab.domain.vo.instrument.SchedulerEventVo;
import org.springframework.beans.BeanUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 维护相关对象的转换
 *
 * @author wmh
 */
public class MaintenanceConverter {

    private MaintenanceConverter() {}

    /**
     * 维护视图对象转化为维护实体对象
     * @param maintenanceVo
     * @return
     */
    public static InstrumentMaintenance maintenanceVoToMaintenance(MaintenanceVo maintenanceVo){
        InstrumentMaintenance maintenance = new InstrumentMaintenance();
        BeanUtils.copyProperties(maintenanceVo,maintenance);
        maintenance.setStartTime(DateConverter.stringToDate(maintenanceVo.getStartTime()));
        maintenance.setEndTime(DateConverter.stringToDate(maintenanceVo.getEndTime()));
        return maintenance;
    }

    /**
     * 维护实体对象转化为维护视图对象
     * @param maintenance
     * @return
     */
    public static MaintenanceVo maintenanceTomaintenanceVo(InstrumentMaintenance maintenance){
        MaintenanceVo maintenanceVo=new MaintenanceVo();
        BeanUtils.copyProperties(maintenance,maintenanceVo);
        maintenanceVo.setStartTime(DateConverter.DateToString(maintenance.getStartTime()));
        maintenanceVo.setEndTime(DateConverter.DateToString(maintenance.getEndTime()));
        return maintenanceVo;
    }

    /**
     * 维护视图对象列表转化为维护实体列表
     * @param maintenanceVos
     * @return
     */
    public static List<InstrumentMaintenance> maintenanceVoListToMaintenanceList(List<MaintenanceVo> maintenanceVos){
        ArrayList<InstrumentMaintenance> maintenances=new ArrayList<>();
        for(MaintenanceVo e : maintenanceVos){
            InstrumentMaintenance maintenance=maintenanceVoToMaintenance(e);
            maintenances.add(maintenance);
        }
        return maintenances;
    }

    /**
     * 维护实体列表转对象换为日程事件视图列表
     * @param maintenances
     * @return
     */
    public static List<SchedulerEventVo> maintenanceListToSchadularEventVoList(List<InstrumentMaintenance> maintenances) {

        ArrayList<SchedulerEventVo> schedulerEventVos=new ArrayList<>();

        Calendar nc=Calendar.getInstance();
        Calendar stc=Calendar.getInstance();
        Calendar ec=Calendar.getInstance();
        SimpleDateFormat dayfomater=new SimpleDateFormat("yyyy/MM/dd");
        SimpleDateFormat timefomater=new SimpleDateFormat("HH:mm:ss");

        nc.setTime(new Date());
        int year = nc.get(Calendar.YEAR);
        int month = nc.get(Calendar.MONTH);
        int date = nc.get(Calendar.DATE);
        int dayOfWeek = nc.get(Calendar.DAY_OF_WEEK);
        int weekOfYear=nc.get(Calendar.WEEK_OF_YEAR);


        String startDay=null;
        String endDay=null;
        String startTime=null;
        String endTime=null;
        for (InstrumentMaintenance maintenance:maintenances){

            SchedulerEventVo sev=new SchedulerEventVo();
            sev.setId(Long.parseLong(CodeUtil.getRandom(8)));

            //每周
            if(maintenance.getType() == 0){
                stc.setWeekDate(year,weekOfYear,maintenance.getStartDay()+1);
                Date stcTime=stc.getTime();
                startDay=dayfomater.format(stcTime);
                startTime=timefomater.format(maintenance.getStartTime());
                sev.setStart_date(startDay+" "+startTime);

                ec.setWeekDate(year,weekOfYear,maintenance.getEndDay()+1);
                Date ecTime=ec.getTime();
                endDay=dayfomater.format(ecTime);
                endTime=timefomater.format(maintenance.getEndTime());
                sev.setEnd_date(endDay+" "+endTime);

                sev.setText("仪器维护时间："+sev.getStart_date()+" ~ "+sev.getEnd_date());
            }
            //每月
            if(maintenance.getType() == 1){
                stc.set(year,month,maintenance.getStartDay());
                Date stcTime=stc.getTime();
                startDay=dayfomater.format(stcTime);
                startTime=timefomater.format(maintenance.getStartTime());
                sev.setStart_date(startDay+" "+startTime);

                ec.set(year,month,maintenance.getEndDay());
                Date ecTime=ec.getTime();
                endDay=dayfomater.format(ecTime);
                endTime=timefomater.format(maintenance.getEndTime());
                sev.setEnd_date(endDay+" "+endTime);

                sev.setText("仪器维护时间："+sev.getStart_date()+" ~ "+sev.getEnd_date());
            }
            //每年
            if(maintenance.getType() == 2){
                stc.set(year,maintenance.getStartMonth()-1,maintenance.getStartDay());
                Date stcTime=stc.getTime();
                startDay=dayfomater.format(stcTime);
                startTime=timefomater.format(maintenance.getStartTime());
                sev.setStart_date(startDay+" "+startTime);

                ec.set(year,maintenance.getEndMonth()-1,maintenance.getEndDay());
                Date ecTime=ec.getTime();
                endDay=dayfomater.format(ecTime);
                endTime=timefomater.format(maintenance.getEndTime());
                sev.setEnd_date(endDay+" "+endTime);

                sev.setText("仪器维护时间："+sev.getStart_date()+" ~ "+sev.getEnd_date());
            }

            schedulerEventVos.add(sev);
        }

        return schedulerEventVos;
    }
}
